home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / cmplibsr.zoo / $flatten1.P < prev    next >
Text File  |  1988-09-15  |  5KB  |  102 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona,1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /* ---------------------------- $flatten1.P ---------------------------- */
  26.  
  27. /* **********************************************************************
  28. $flatten1_export([$add_uni_hdr/6,$flatten1/8]).
  29.  
  30. $flatten1_use($blist,[$append/3,_,$member1/2]).
  31. $flatten1_use($listutil1,[_,_,_,_,_,_,$member2/2,_]).
  32. $flatten1_use($glob,[_,$gennum/1,$gensym/2]).
  33. $flatten1_use($meta,[_,_,$length/2]).
  34. ********************************************************************** */
  35.  
  36.  
  37. /* "$add_uni_hdr" adds the unifications resulting from the flattening of
  38.     nested structures in the head to the body of the clause.        */
  39.  
  40. $add_uni_hdr([],_,_,_,TGoal, TGoal).
  41. $add_uni_hdr([(V=S)|UniRest],A,NVars,Vlist,
  42.         TGoal, and('_call'(=,[V,S],NV), _, UGoal1)) :-
  43.     V = v(Vid,Prag), $member1(v(Vid,OccList),Vlist), Prag = vrec(_,_,_,_),
  44.     $gennum(Vno), $member1(o(Vno,1,1,A,t,Prag),OccList),
  45.     A1 is A+1, $member1(nv(NVars),NV),
  46.     $add_uni_hdr(UniRest,A1,NVars,Vlist,TGoal, UGoal1).
  47.  
  48. /*     "flatten" flattens each top-level argument, if necessary.    */
  49.  
  50. $flatten(Var, _, _, _, _, Var, _, _) :- Var = v(_, _).
  51. $flatten([Funcsym|ArgList],Vlist,Path,Lit,Arg,
  52.             [Funcsym|NArgList],UniList,BindList) :-
  53.     $flatten1(ArgList,Vlist,Path,Lit,Arg,NArgList,UniList,BindList).
  54.  
  55. /* "$flatten1" is given a list of the arguments of any structure at the top
  56.    level.  Any structure found in this list is nested and has to be flattened.
  57.    This flattening is done by flatten2, flatten1 just loops through calling
  58.    flatten2 with each of the arguments in the list given.        */
  59.  
  60. $flatten1([], _,_, _, _, [], _,_).
  61. $flatten1([H|T],Vlist,Path,Lit,Arg, [FH|FT], UniList,BindList) :-
  62.     $flatten2(H,Vlist,Path,Lit,Arg, FH, UniList,BindList),
  63.     $flatten1(T, Vlist,Path,Lit,Arg, FT, UniList,BindList).
  64. $flatten2(Var, _,_,_, _, Var, _,_) :- Var = v(_, _), !.
  65. $flatten2(Cons,_,_,_, _, Cons, _,_) :-
  66.     $length(Cons, L),
  67.     L < 2,
  68.     !.
  69. $flatten2(Str, Vlist, Path, Lit,Arg, v(Temp, Prag), UniList,BindList) :-
  70.     $flatten_make_var(Str,Vlist,UniList,
  71.                 BindList,Path,Lit,Arg,v(Temp,Prag),FStr),
  72.     $flatten(Str, Vlist, Path, Lit, Arg, FStr, UniList,BindList).
  73.  
  74. /* "$flatten_make_var" is called with a structure Str, and returns a variable
  75.     that replaces it in the process of flattening of nested structures. It
  76.     first searches BindList to see whether the structure has already been
  77.     flattened: if so, it returns the variable that had replaced it earlier,
  78.     and enters occurrence info for the variable, as well as other such
  79.     variables inside the flattened structure, into the symbol table: this may
  80.     result in some temporary variables becoming permanent. If no such variable
  81.     is found, a new one is generated.                    */
  82.  
  83. $flatten_make_var(Str,Vlist,UniList,BindList,Path,Lit,Arg,v(Vid,Prag),_) :-
  84.         $member2((v(Vid, _),Str1),BindList),
  85.         $flatten_same_str(Str, Str1), Prag = vrec(_,_,_,_),
  86.         $member1(v(Vid, OccList), Vlist), $gennum(N),
  87.         $member1(o(N,Path,Lit,Arg,s,Prag), OccList).
  88.  
  89. $flatten_make_var(Str,Vlist,UniList,BindList,Path,Lit,Arg,v(Vid,Prag),FStr) :-
  90.         $gensym('$',Vid), $gennum(N),
  91.         $member1(v(Vid, [o(N,Path,Lit,Arg,s,Prag)|_]), Vlist),
  92.         $member1((v(Vid,_),Str),BindList), Prag = vrec(_,_,_,_),
  93.         $member1((v(Vid,_) = FStr), UniList).
  94.  
  95. $flatten_same_str([], []).
  96. $flatten_same_str(v(Vid, X), v(Vid, Y)).
  97. $flatten_same_str([Func|Args1], [Func|Args2]) :-
  98.     $flatten_same_str(Args1, Args2).
  99.  
  100. /* ---------------------------- $flatten1.P ---------------------------- */
  101.  
  102.